<?php
/**
 * Created by PhpStorm.
 * User: Will
 * Date: 2018/1/15
 * Time: 19:13
 */
date_default_timezone_set('Asia/shanghai');
class uploadModel extends Model
{

    public function getTypeAction(){
        $sql = "select * from  activitytype";
        $str = $this->link->selectDB($sql);
        echo json_encode($str);
    }
    public function uploadAction(){
        $user = isset($_SESSION['user']) ? $_SESSION['user']['id'] : 0;
        $data = isset($_POST['data']) ? $_POST['data'] : null;
//        echo json_encode(['code'=>200,'data'=>$data]);
        $t = date($data['date']);
//        echo json_encode(['code'=>$t]);
        $time = date('y-m-d h:i:s',time());
        $sql = "INSERT INTO `xiaojv`.`activity` ( `activityName`, `activityContent`, `activityType`,`activityDate`, `activityLimit`, `tel`, `uid`, `activityNum`,  `num`, `img`, `lastDate`, `releaseDate`) VALUES ('{$data['title']}', '{$data['content']}', '{$data['type']}', '{$time}', '{$data['sex']}', '{$data['tel']}', '1', '{$data['pro']}','0', NULL, '{$data['ldate']}', '{$data['date']}')";

        $str = $this->link->query($sql);
        if($str){
            echo json_encode(['code'=>200,'msg'=>'发布成功,请等待审核']);
        }else{
            echo json_encode(['code'=>201,'msg'=>'发布失败','sql'=>$sql]);
        }


    }

    public function uploadimgAction(){
        $fileName = $_FILES["img"]["name"];
        $pro = isset($_POST['pro']) ? $_POST['pro'] : null;
        $title = isset($_POST['title']) ? $_POST['title'] : null;
        $content = isset($_POST['content']) ? $_POST['content'] : null;
        $sex = isset($_POST['sex']) ? $_POST['sex'] : null;
        $tel = isset($_POST['tel']) ? $_POST['tel'] : null;
        $date = isset($_POST['date']) ? $_POST['date'] : null;
        $ldate = isset($_POST['ldate']) ? $_POST['ldate'] : null;
        $type = isset($_POST['type']) ? $_POST['type'] : null;

        $upload_path = './public/upload/';
        if(!file_exists($upload_path)){
            mkdir($upload_path);
        }
        $time = date('y-m-d h:i:s',time());
        $newname = substr_replace($fileName, date("YmdHis") . rand(100, 999), 0, strrchr(".", $fileName)); //重新命名
        $url = $upload_path . $newname;
        if (move_uploaded_file($_FILES["img"]["tmp_name"], $url)) {
            $ip = $_SERVER['SERVER_ADDR'];
            $url="http://10.2.49.1/xiaojv/api/public/img/". $newname;
            $sql = "INSERT INTO `xiaojv`.`activity` ( `activityName`, `activityContent`, `activityType`,`activityDate`, `activityLimit`, `tel`, `uid`, `activityNum`,  `num`, `img`, `lastDate`, `releaseDate`) VALUES ('{$title}', '{$content}', '{$type}', '{$time}', '{$sex}', '{$tel}', '1', '{$pro}','0', '{$url}', '{$ldate}', '{$date}')";
            $str = $this->link->query($sql);

            if($str){
                echo json_encode(['code'=>200,'msg'=>'发布成功，请等待审核']);
            }else{
                echo json_encode(['code'=>202,'msg'=>'发布失败','sql'=>$sql]);
            }

        }else{
            echo json_encode(['code'=>201,'msg'=>'发布失败']);
        }




    }
}

